package com.doubtech.ahome.widgets.openclock;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.R.color;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OpenClockWidget extends LinearLayout implements MultiInstanceSupport, LifeCycleSupport {
	private boolean mBroadcastRegistered = false;
	
	/*
	 * HINT if your widget don't support multi-instance, implementing MultiInstanceSupport
	 * is not required
	 */ 
	private int mId;
	private boolean mDataLoaded = false;
	
	private TextView mTime;
	private TextView mDate;
	private Thread notifyingThread;
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			UpdateTime();
		}
	};
	
	private Runnable mTask = new Runnable() {
        public void run() {
        	while(true)
        	{
        		try {
            		handler.sendEmptyMessage(0);
					Thread.sleep(60000);
				} catch (InterruptedException e) {
					break;
				} catch (Exception e) {
					Log.e(MainActivity.LOG_TAG, "Error: " + e.getMessage(), e);
				}
        	}
        }
    };
	
	public OpenClockWidget(Context context) {
		super(context);
	}
	
	public OpenClockWidget(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		// HINT you can do some initialization here
	}
	
	public void onDestory() {
	    /*
	     * TODO Do some clean work
	     * If you don't care destory event, no need to implement LifeCycleSupport interface
	     */
		
		Log.d("aHome", "OpenClockWidget Widget " + mId + " is destoryed");
	}
	
	@Override
	protected void onFinishInflate() {
	    super.onFinishInflate();
	    
	    /* 
	     * HINT you can do some initialization here, but don't load
	     * instance-specified data here because setInstanceId() is 
	     * called after onFinishInflate()
	     */
        
	    mTime = (TextView)findViewById(R.id.time);
	    mDate = (TextView)findViewById(R.id.date);
	    Log.d(MainActivity.LOG_TAG, "Starting thread.");
	    notifyingThread = new Thread(null, mTask, "NotifyingService");
        notifyingThread.start();
	}
	
	@Override
	protected void onAttachedToWindow() {
	    super.onAttachedToWindow();
	    
	    // HINT you can do some initialization here
	    
	    if(!mBroadcastRegistered) {
	    	/*
	    	 * TODO register your broadcast receiver here, if you want
	    	 */ 
	    	
	    	mBroadcastRegistered = true;
	    }
	    
	    /*
	     * HINT if your widget supports multi-instance, you can load data here
	     * to avoid loading data more than one time, you can set a flag, such as
	     * mDataLoaded I used
	     */
	    if(!mDataLoaded) {
	    	// TODO load your instance-specific data and initialize instance-specific content
	    	UpdateTime();    	
	    	mDataLoaded = true;
	    }
	}
	
	private void UpdateTime() {
		Date now = new Date();
    	SimpleDateFormat format = new SimpleDateFormat("h:mm");
    	mTime.setText(format.format(now));
    	mTime.setShadowLayer(2f, .1f, .1f, color.darker_gray);
    	mTime.setTypeface(Typeface.DEFAULT_BOLD);
    	mDate.setShadowLayer(2f, .1f, .1f, color.darker_gray);
    	format = new SimpleDateFormat("EEEE\nMMM d, yyyy");
    	mDate.setText(format.format(now));	
	}

	@Override
	protected void onDetachedFromWindow() {
		// HINT you can do some clean work here
		
		if(mBroadcastRegistered) {
			/*
			 * TODO unregister your broadcast here if you have some
			 * 
			 * You need check mBroadcastRegistered flag because onDetachedFromWindow may be
			 * called continuously. If you don't set a flag, you will get exception at second
			 * onDetachedFromWindow calling, and it crashes aHome also!
			 */
			
			
			mBroadcastRegistered = false;
		}
		
	    super.onDetachedFromWindow();
	}

	public void setInstanceId(int id) {
		mId = id;
    }
}
